/**
 * Created by admin on 4/27/16.
 */
Home.Table = function(options){
    if (!(options.actions && options.columns && options.searchers && options.url && options.fields)){
        //throw 'arguments options.actions,options.columns,options.searchers,options.url,options.fields is needed.'
    }

    var
        // 获取查找参数,通过获取数据的ajax请求,一并传回到服务器
        getParams = function(){
            return {}
        },
        // 生成表格
        make_modal = function(fields){
            var main_html = String() +
                    '<div class="modal fade bs-modal-lg" tabindex="-1" role="dialog" aria-hidden="true">'+
                        '<div class="modal-dialog modal-lg">'+
                            '<div class="modal-content">'+
                                '<div class="modal-body">'+
                                    '<div class="portlet box green">'+
                                        '<div class="portlet-title">'+
                                            '<div class="caption">'+
                                                '<i class="fa fa-edit"></i><span class="title">编辑</span>'+
                                            '</div>'+
                                        '</div>'+
                                        '<div class="portlet-body form">'+
                                            '<form role="form" class="form-horizontal">'+
                                                '<div class="form-body">'+
                                                '</div>'+
                                            '</form>'+
                                        '</div>'+
                                    '</div>'+
                                '</div>'+
                                '<div class="modal-footer">'+
                                    '<button type="button" class="btn default" data-dismiss="modal">放弃</button>'+
                                    '<button type="button" class="btn green" id="post_search">确定</button>'+
                                '</div>'+
                            '</div>'+
                        '</div>'+
                    '</div>'
            return $(main_html)
        },

        make_table = function(fields){
            var main_html = String() +
                  '<table class="table table-bordered" width="100%" cellspacing="0">'+
                        '<thead>'+
                            '<tr>' +
                            '</tr>' +
                        '</thead>' +
                  '</table>',result,$tr,$th;
            result = $(main_html)
            $tr = result.find("thead tr")
            for(index in fields){
                $th = "<th>" + fields[index].title + "</th>"
                $th = $($th)
                $tr.append($th)
            }

            return result
        },

        wraps = [
            function(method){
                return function(event){
                    method('params1')
                    $(this).parents("li.dropdown").trigger("click")
                    return false
                }
            },
            function(method){
                return function(event){
                    method('params1','params2')
                    $(this).parents("li.dropdown").trigger("click")
                    return false
                }
            },
            function(method){
                return function(event){
                    method('params1','params2','params3')
                    $(this).parents("li.dropdown").trigger("click")
                    return false
                }
            }
        ],

        make_actions = function(actions){
            var result=[],$li,$ul
            for(index in actions){
                if (actions[index].name==undefined){
                    $li = []
                    $li.push('<li class="dropdown"><a href="return false;" class="dropdown-toggle" data-toggle="dropdown">'+actions[index].title+'<b class="caret"></b></a>')
                    $li.push('<ul class="dropdown-menu">')
                    $li.push('</ul>')
                    $li.push('</li>')
                    $li = $($li.join(''))
                    $ul = $li.find("ul")
                    var sub_lis = make_actions(actions[index].children)
                    for(j in sub_lis){
                        $ul.append(sub_lis[j])
                    }
                    result.push($li)
                } else {
                    $li = $('<li><a href="return false" name="'+ actions[index].name +'">' + actions[index].title +'</a></li>')
                    $li.find("a").click(wraps[parseInt(actions[index].params)](actions[index].action))
                    result.push($li)
                }
            }
            return result
        },

        make_action_lis = function(actions){
            var $menu = make_actions(actions)
            for(index in $menu){
                jqueryMap.$action_container.append($menu[index])
            }
            return $menu
        }

        configMap = {
            columns : options.columns,
            actions : options.actions,
            fields : options.fields,
            search_fields : options.search_fields,

            defaults : {
                dom: "<'table-scrollable't><'row pagination-panel'<'col-md-2 col-sm-12'l><'col-md-4 col-sm-12'i><'col-md-6 col-sm-12'p>>",
                processing: true,
                serverSide: true,
                scrollCollapse: false,
                filter: false,
                displayLength: 20,
                searching: false,
                lengthMenu: [
                    [20, 100, 150, 200, 250, 300, 400, 500, 600, -1],
                    [20, 100, 150, 200, 250, 300, 400, 500, 600, "All"]
                ],
                oLanguage: {
                    oAria: {
                        sSortAscending: ": 升序排列",
                        sSortDescending: ": 降序排列"
                    },
                    oPaginate: {
                        sFirst: "首页 ",
                        sLast: " 末页",
                        sNext: " 下页",
                        sPrevious: "上页 "
                    },
                    sEmptyTable: "没有相关记录",
                    sInfo: "第 _START_ 到 _END_ 条记录，共 _TOTAL_ 条",
                    sInfoEmpty: "第 0 到 0 条记录，共 0 条",
                    sInfoFiltered: "(从 _MAX_ 条记录中检索)",
                    sInfoPostFix: "",
                    sDecimal: "",
                    sThousands: ",",
                    sLengthMenu: "每页&nbsp;_MENU_&nbsp;条",
                    sLoadingRecords: "正在载入...",
                    sProcessing: "正在载入...",
                    sSearch: "搜索:",
                    sSearchPlaceholder: "",
                    sUrl: "",
                    sZeroRecords: "没有相关记录"
                },
                oPaginationType: "full_numbers",
                ajax: {
                    url: options.url,
                    type: "post",
                    data: getParams()
                },
                aaSorting: [],
                select : true
            },
        },

        jqueryMap = {
            $table_container : options.$table_container,
            $action_container : options.$actions_container,
            $footer_container : options.$footer_container
        },

        stateMap = {
            $datatable : null
        },

        init = function(){
            var defaults = $.extend({},configMap.defaults)
            defaults['columns'] = configMap.columns
            jqueryMap.$table = make_table(configMap.columns)
            jqueryMap.$searcher_modal = make_modal(configMap.search_fields)
            jqueryMap.$editor_modal = make_modal(configMap.fields)
            jqueryMap.$actions = make_action_lis(configMap.actions)
            options.$table_container.html("")
            options.$table_container.append(jqueryMap.$table)
            stateMap.$datatable = jqueryMap.$table.DataTable(defaults)
        }

    init()

    self.show = function(){
        jqueryMap.$table_container.html('')
        jqueryMap.$table_container.append(jqueryMap.$table)
    }
}